package com.codechuan.hallo.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.codechuan.hallo.dto.CityInfo;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;

public class CommonUtil {
    public static void main(String[] args) {
        System.out.println(getCityInfo("四川省成都市武侯区"));
    }
    /**
     * @Deprecated 通过位置获取经纬度等信息接口
     * @param CityName 起点位置名称
     */
    public static CityInfo getCityInfo(String CityName){
        String encode = URLEncoder.encode(CityName);
        String url = "https://apis.map.qq.com/ws/place/v1/search?keyword=" + encode + "&boundary=region(,0)&page_size=9&page_index=1&key=6KQBZ-IJS6Z-46PXF-TVMYQ-VX6AV-XMB3Q";
        String result = null;
        try {
            result = OkHttpUtil.doGet1(url);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        JSONObject parseObject = JSONObject.parseObject(result);
        JSONArray dataList = JSONObject.parseArray(parseObject.getString("data"));
        if (dataList.isEmpty())
            throw new RuntimeException("位置设置错误");
        JSONObject one = JSONObject.parseObject(dataList.get(0).toString());
        String city = JSONObject.parseObject(one.getString("ad_info")).getString("city");
        String adCode = JSONObject.parseObject(one.getString("ad_info")).getString("adcode");
        String lat = JSONObject.parseObject(one.getString("location")).getString("lat");
        String lng = JSONObject.parseObject(one.getString("location")).getString("lng");
        String cityId = "";
        try {
            boolean isJump = false;

            String json = new String(Files.readAllBytes(Paths.get("C:\\Users\\Administrator\\Desktop\\city.txt")));
            // 打印读取到的JSON内容
            JSONObject jsonObject = JSONObject.parseObject(json);
            JSONArray jsonArray = JSONObject.parseArray(jsonObject.getString("groups"));
            for (int i = 0; i < jsonArray.size(); i++) {
                if (i != 0) {
                    if (isJump)
                        break;
                    JSONObject object = JSONObject.parseObject(jsonArray.get(i).toString());
                    String cities = object.getString("cities");
                    JSONArray citiArray = JSONObject.parseArray(cities);
                    for (Object o : citiArray) {
                        JSONObject jsonObject1 = JSONObject.parseObject(o.toString());
                        if (jsonObject1.getString("name").equals(city)) {
                            cityId = jsonObject1.getString("cityid");
                            isJump = true;
                            break;
                        }
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        CityInfo cityInfo = new CityInfo();
        cityInfo.setCity(city);
        cityInfo.setLng(lng);
        cityInfo.setLat(lat);
        cityInfo.setCityId(cityId);
        // 通过城市名称获取邮编和城市区号
        cityInfo.setAdCode(adCode);
        return cityInfo;
    }
}
